package com.qs.questionnairesurveysystem.controller;

import com.qs.questionnairesurveysystem.dto.Result;
import com.qs.questionnairesurveysystem.entity.Admin;
import com.qs.questionnairesurveysystem.service.AdminService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
@Api(tags = "Admin Management", description = "Operations related to managing admins")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "Get Admin by ID", notes = "Retrieve an admin's details by admin ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved admin"),
            @ApiResponse(code = 404, message = "Admin not found")
    })
    @GetMapping("/{adminId}")
    public Result<Admin> getAdminById(@PathVariable Long adminId) {
        Admin admin = adminService.getAdminById(adminId);
        if (admin == null) {
            return Result.error("Not Found", 404);
        }
        return Result.success(admin);
    }

    @ApiOperation(value = "Get All Admins", notes = "Retrieve a list of all admins.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved list of admins")
    })
    @GetMapping("/")
    public ResponseEntity<List<Admin>> getAllAdmins() {
        List<Admin> admins = adminService.getAllAdmins();
        return ResponseEntity.ok(admins);
    }

    @ApiOperation(value = "Create a new Admin", notes = "Create a new admin in the system.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully created admin"),
            @ApiResponse(code = 500, message = "Internal server error")
    })
    @PostMapping("/")
    public ResponseEntity<Admin> createAdmin(@RequestBody Admin admin) {
        int result = adminService.createAdmin(admin);
        if (result > 0) {
            return ResponseEntity.ok(admin);
        }
        return ResponseEntity.status(500).build();
    }

    @ApiOperation(value = "Update an Admin", notes = "Update an existing admin by admin ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully updated admin"),
            @ApiResponse(code = 500, message = "Internal server error")
    })
    @PutMapping("/{adminId}")
    public ResponseEntity<Admin> updateAdmin(@PathVariable Long adminId, @RequestBody Admin admin) {
        admin.setAdminId(adminId);
        int result = adminService.updateAdmin(admin);
        if (result > 0) {
            return ResponseEntity.ok(admin);
        }
        return ResponseEntity.status(500).build();
    }

    @ApiOperation(value = "Delete an Admin", notes = "Delete an admin by admin ID.")
    @ApiResponses(value = {
            @ApiResponse(code = 204, message = "Successfully deleted admin"),
            @ApiResponse(code = 500, message = "Internal server error")
    })
    @DeleteMapping("/{adminId}")
    public ResponseEntity<Void> deleteAdmin(@PathVariable Long adminId) {
        int result = adminService.deleteAdmin(adminId);
        if (result > 0) {
            return ResponseEntity.noContent().build();
        }
        return ResponseEntity.status(500).build();
    }
}
